package com.example.myapplication.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.AlbumActivity;
import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.adapter.ResultAlbumAdapter;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.Hots;
import com.example.myapplication.entity.SearchResult;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ResultAlbumFragment extends Fragment {
    private RecyclerView recyclerView;
    private Context context;
    private Hots hots;
    private ResultAlbumAdapter adapter;
    private SearchResult result;
    private List<SearchResult.AlbumsBean> albums;
    private String keyword;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(container.getContext()).inflate(R.layout.layout_recycle_view, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context = getContext();
        recyclerView = view.findViewById(R.id.recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        adapter = new ResultAlbumAdapter(context);
        recyclerView.setAdapter(adapter);
        Bundle bundle = getArguments();

        if (bundle.getString("search_input") != null){
            keyword = bundle.getString("search_input");
        }
        else {
            hots = bundle.getParcelable("search_key");
            keyword = hots.getSearchWord();
        }

        loadData(keyword, 10);

        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(context, AlbumActivity.class);
                intent.putExtra("albumID", albums.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void loadData(String keyword, int type){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponse> call = api.getResultSinger(type, keyword);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();

                result = baseResponse.getResult();
                albums = result.getAlbums();
                adapter.setData(albums);

            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static ResultAlbumFragment newInstance(Bundle bundle){
        ResultAlbumFragment fragment = new  ResultAlbumFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
